﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using AutoMapper;
using DotNetCore.CAP;
using Q.M.Finance.Application.IServices;
using Q.M.Finance.Domain.DTO;
using Q.M.Finance.Domain.Entity;
using Q.M.Finance.Infrastruecure.IRepository;
using Q.M.Finance.Infrastruecure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Q.M.Finance.Application.Services
{
    public class Billing_managementServices : BaseServices<billing_management, string>, IBilling_managementServices
    {
        private readonly IBilling_managementRepository billing_ManagementRepository;
        private readonly IMapper mapper;
        private readonly ICapPublisher capPublisher;
        private readonly IInvoice_typeRepository invoice_TypeRepository;
        public Billing_managementServices(IInvoice_typeRepository invoice_TypeRepository,ICapPublisher capPublisher,IMapper mapper,IBilling_managementRepository billing_ManagementRepository) : base(billing_ManagementRepository)
        {
            this.billing_ManagementRepository= billing_ManagementRepository;
            this.mapper = mapper;
            this.capPublisher = capPublisher;
            this.invoice_TypeRepository = invoice_TypeRepository;
        }
        /// <summary>
        /// 添加开票管理
        /// </summary>
        /// <param name="billing_ManagementAddDTO"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> Getbilling_managementAddInfo(billing_managementAddDTO billing_ManagementAddDTO)
        {
            try
            {
                ApiReturn<string> apiReturn = new ApiReturn<string>();
                var billing_management = mapper.Map<billing_management>(billing_ManagementAddDTO);
                var res = await billing_ManagementRepository.AddInfor_Async(billing_management);
                if (res > 0)
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "添加成功";
                    //添加成功需要传一个消息队列  传递哪个模块 还有编号
                    string numberName = "开票系统-" + billing_ManagementAddDTO.invoiceid;
                    capPublisher.Publish("GetNewNumber", numberName);
                }
                else
                {
                    apiReturn.Code = 400;
                    apiReturn.Message = "添加失败";

                }
                return apiReturn;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改开票管理
        /// </summary>
        /// <param name="incomeUpdateDTO"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> Getbilling_managementUpdateInfo(billing_managementShowDTO incomeUpdateDTO)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var id = billing_ManagementRepository.GetShowInfor(x => x.invoiceid.Equals(incomeUpdateDTO.invoiceid));
            foreach (var item in id)
            {
                item.invoiceid = incomeUpdateDTO.invoiceid;
                item.billing_subject = incomeUpdateDTO.billing_subject;
                item.invoicing_typename = incomeUpdateDTO.invoicing_typename;
                item.date_of_issue = incomeUpdateDTO.date_of_issue;
                item.payee = incomeUpdateDTO.payee;
                item.invoice_number = incomeUpdateDTO.invoice_number;
                item.invoice_amount = incomeUpdateDTO.invoice_amount;
            }
            var res = await billing_ManagementRepository.UpdInfor_Batch_Async(id);
            if (res > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "修改成功";
            }
            return apiReturn;
        }
        /// <summary>
        /// 反填开票管理
        /// </summary>
        /// <param name="invoiceid"></param>
        /// <returns></returns>
        public async Task<billing_management> Findbilling_managementInfo(string? invoiceid)
        {
            Expression<Func<billing_management, bool>> i = c => c.invoiceid == invoiceid;
            var sales = billing_ManagementRepository.GetShowObj(i);
            return sales;
        }

        /// <summary>
        /// 删除开票管理
        /// </summary>
        /// <param name="invoiceid"></param>
        /// <returns></returns>
        public async Task<ApiReturn<string>> Deletebilling_management(string? invoiceid)
        {
            Expression<Func<billing_management, bool>> i = c => c.invoiceid == invoiceid;
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var income = billing_ManagementRepository.GetShowObj(i);
            var list = await billing_ManagementRepository.DelInfor_Async(income);
            if (list != null)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "删除成功!";
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "删除失败!";
            }
            return apiReturn;
        }
        /// <summary>
        /// 收入开票显示+分页+查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageETO<billing_managementShowDTO> Getbilling_managementInfo(string? invoiceid, int pageIndex, int pageSize)
        {
            PageETO<billing_managementShowDTO> pageETO = new PageETO<billing_managementShowDTO>();
            Expression<Func<billing_management, bool>> wherexp = i => true;
            if (invoiceid != null)
            {
                wherexp = i => i.invoiceid == invoiceid;
            }
            var incomelist = billing_ManagementRepository.GetShowInfor(wherexp).ToList();
            var res = invoice_TypeRepository.GetShowInfor();
            var list = (from a in incomelist
                                  join b in res on a.invoicing_typename equals b.invoicing_typename
                                  select new billing_managementShowDTO
                                  {
                                      invoiceid = a.invoiceid,
                                      billing_subject = a.billing_subject,
                                      invoicing_typename = b.invoicing_typename,
                                      date_of_issue = a.date_of_issue,
                                      payee = a.payee,
                                      invoice_number = a.invoice_number,
                                      invoice_amount = a.invoice_amount,
                                  }).ToList();
            pageETO.TotalCount = list.Count();
            pageETO.PageCount = (int)Math.Ceiling(pageETO.TotalCount * 1.0 / pageSize);
            pageETO.Data = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageETO;
        }
    }
}
